using System;
using System.Collections.Generic;
using System.Linq;
using Survival;
using Survival.Services;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Survival.Towns
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Town : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Dictionary<string, Texture2D> spriteDictionary;
        List<Building> buildingList;
        SpriteBatch buildingBatch;

        public Town(Game game)
            : base(game)
        {
            spriteDictionary = new Dictionary<string, Texture2D>();
            buildingList = new List<Building>();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            buildingBatch = new SpriteBatch(GraphicsDevice);
            buildingList.Add(new CityHall(new Vector2(600,200),this));
        }

        protected override void LoadContent()
        {
            spriteDictionary.Add("CityHall", Game.Content.Load<Texture2D>("CityHall"));

            base.LoadContent();
        }


        public override void Draw(GameTime gameTime)
        {
            Texture2D currentSprite;

            buildingBatch.Begin();
            foreach (Building building in buildingList)
            {
                spriteDictionary.TryGetValue(building.SpriteName, out currentSprite);
                Vector2 spriteCenter = new Vector2(currentSprite.Bounds.Center.X, currentSprite.Bounds.Center.Y);
                buildingBatch.Draw(currentSprite, building.Position, null, Color.White, 0, spriteCenter, 1f, SpriteEffects.None, 0);
            }
            buildingBatch.End();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            foreach (Building building in buildingList)
            {
                building.Update(gameTime);
            }

            base.Update(gameTime);
        }
    }

    public struct Integrity_t
    {
        int currentIntegrity;
        int maxIntegrity;

        public Integrity_t(int current, int max)
        {
            currentIntegrity = current;
            maxIntegrity = max;
        }
    }
}
